//
//  LRComposeToolBar.swift
//  WeiboX
//
//  Created by admin on 10/12/2016.
//  Copyright © 2016 iOS_Objective-C. All rights reserved.
//

import UIKit

enum LRComposeToolBarBtnType: Int {
	case camera = 0
	case mention = 1
	case trend = 2
	case emoticon = 3
	case add = 4
}

class LRComposeToolBar: UIStackView {
	
	let imgNameArr: [String] = ["compose_camerabutton_background",
	                            "compose_mentionbutton_background",
	                            "compose_trendbutton_background",
	                            "compose_emoticonbutton_background",
	                            "compose_add_background"
							    ]
	
	var emoticonBtn: UIButton?
	/*{
	var emoticonBtn: UIButton = {
		let btn = UIButton()
		
		btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
		
		btn.tag = 3
		
		btn.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
		btn.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
		btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
		
		btn.adjustsImageWhenHighlighted = false
		
		return btn
	}()
	}*/
	var callBack: ((LRComposeToolBarBtnType) -> ())?
	
	override init(frame: CGRect) {
		super.init(frame: frame)
		setupUI()
	}
	
	required init(coder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	private func setupUI() {
//		emoticonBtn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
		axis = .horizontal
		distribution = .fillEqually
		
//		for (i, imgName) in imgNameArr.enumerated() {
//			addChildWidgets(imgName: imgName, type: LRComposeToolBarBtnType(rawValue: i)!)
//		}
		
		addChildWidgets(imgName: "compose_camerabutton_background", type: .camera)
		addChildWidgets(imgName: "compose_mentionbutton_background", type: .mention)
		addChildWidgets(imgName: "compose_trendbutton_background", type: .trend)
//		addChildWidgets(imgName: "compose_emoticonbutton_background", type: .emoticon)
		emoticonBtn = addChildWidgets(imgName: "compose_emoticonbutton_background", type: .emoticon)
		addChildWidgets(imgName: "compose_add_background", type: .add)
		
//		addArrangedSubview(emoticonBtn)
	}
	
	@discardableResult
	private func addChildWidgets(imgName: String, type: LRComposeToolBarBtnType) -> UIButton {
		let btn = UIButton()
		
		btn.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
		
		btn.tag = type.rawValue
		
		btn.setImage(UIImage(named: imgName), for: .normal)
		btn.setImage(UIImage(named: imgName + "_highlighted"), for: .highlighted)
		btn.setBackgroundImage(UIImage(named: "compose_toolbar_background"), for: .normal)
		
		btn.adjustsImageWhenHighlighted = false
		addArrangedSubview(btn)
		
		return btn
	}
	
	@objc private func btnAction(btn: UIButton) {
		
		let type = LRComposeToolBarBtnType(rawValue: btn.tag)!
		
		callBack?(type)
	}
	
	func switchEmoticonIcon (isEmoticon: Bool) {
		if isEmoticon {
			// 设为键盘 icon
			emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background"), for: .normal)
			emoticonBtn?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), for: .highlighted)
		} else {
			// 设为表情 icon
			emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background"), for: .normal)
			emoticonBtn?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), for: .highlighted)
		}
	}
}
